import urllib.request
import requests
from bs4 import BeautifulSoup
import os

header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}


useplace = ['时装', ' 男装', ' 女装', ' 童装', '老年装', '家纺', '方巾(围巾)', 'T-Shirt', '泳装', '睡衣', '内衣',  # 用途
            '窗帘', '墙纸墙布', '沙发', '牛仔', '床上用品', '包包']
style = ['纹理', '风景', '休闲风', '虎纹', '腰果花', '抽象花型', '传统花型', '豹纹', '蛇皮', '几何', '定位', '花卉',  # 风格
         '民族风', '格子', '人物', '卡通', '小碎花', '动物花型', '迷彩']
# character = ['数码花型', '传统花型', '平接', '跳接']  # 特性


def grap_image(url,images_path):
    i = 1
    next_page = 1
    next_query = 1
    while True:
        url_i = url + '&page=' + str(i)
        response_i = requests.get(url_i, headers=header)
        print(url_i)

        # 获取第i个页面的url、response类、html、soup，以及该页面所有图片对应的src
        html_i = response_i.text

        html_soup_i = BeautifulSoup(html_i, 'html.parser')


        all_img_links_i = html_soup_i.find_all('div', attrs={'class': 'imgholder'})
        # print(len(all_img_links_i))
        if len(all_img_links_i)>0:
            for ul in all_img_links_i:
                title = ul.find('a')['title']
                img = ul.find('a').find('img')['data-original']
                print(img)
                print(title)

                img_file_name = '%s.jpg' % title

                path = images_path + img_file_name
                # try:  # 创建或判断路径图片是否存在并下载
                if not os.path.exists(path):
                    r = requests.get(img)
                    with open(path, 'wb') as f:
                        f.write(r.content)
                        f.close()
                        # print("文件保存成功")
                else:
                    next_page = 0
                    break
        else:
            next_query == 0
            break
        if next_page == 0:
            break
        else:
            i += 1


def main():
    for useId in useplace:
        for styleId in style:
                finalUrl = 'http://www.tulaiwang.com/index.php?do=goodslist&m=6&pd=' + useId + '&i=' + \
                           styleId
                images_path = '/Users/chenminli/实验室知识储备/images/' + useId + '-' + styleId +'/'
                # text_path = '/Users/chenminli/Desktop/texts/' + useId + '-' + styleId + '/'
                if not os.path.exists(images_path):
                    os.makedirs(images_path)
                # if not os.path.exists(text_path):
                #     os.makedirs(text_path)

                # grap_image(finalUrl,images_path,text_path)
                grap_image(finalUrl, images_path)


if __name__ == '__main__':
    main()
